package com.botao.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.botao.cms.poi.ExportUserInFo;
import com.botao.cms.pojo.User;
import com.botao.cms.service.UserService;
import com.botao.cms.utils.AjaxResult;
import com.botao.cms.utils.DateUtil;
import com.botao.cms.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Slf4j
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    ExportUserInFo exportUserInFo;

    @GetMapping(path = {"admin/sys/data_monitoring", "admin/sys/data_monitoring.html"})
    public String sysLog() {
        return "admin/sys/data_monitoring";
    }

    @GetMapping(path = {"admin/sys/user"})
    public String sysUser(Model model, Long currentPage, Long pageSize) {

        //从数据库获取
        Page<User> userList = userService.page(new Page<User>(currentPage, pageSize), new QueryWrapper<User>().orderByDesc("state"));
        assert userList != null;
        List<User> records = userList.getRecords();
        long total = userList.getTotal();
        long current = userList.getCurrent();
        boolean hasNext = userList.hasNext();
        model.addAttribute("userList", records);
        model.addAttribute("total", total);
        model.addAttribute("current", current);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("hasNext", hasNext);

        return "admin/sys/user";
    }

    @GetMapping(path = {"admin/sys/user_add"})
    public String userAdd() {
        return "admin/sys/user_add";
    }

    @GetMapping(path = {"admin/sys/user_edit/{id}"})
    public String userEdit(@PathVariable String id, Model model) {
        User user = userService.getById(id);
        if (user == null) {
            user = new User();
        }
        model.addAttribute("user", user);
        return "admin/sys/user_edit";
    }


    @ResponseBody
    @PostMapping(path = {"admin/sys/user_edit"})
    public AjaxResult updateUserById(String id, String username, String sex, String birthday,
                                     String email, String state) throws ParseException {
        User user = new User();
        user.setId(id)
                .setUsername(username)
                .setSex(sex)
                .setBirthday(DateUtil.strToDate(birthday))
                .setEmail(email)
                .setState(Integer.parseInt(state));
        try {
            boolean b = userService.updateById(user);
            return b ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    @ResponseBody
    @PostMapping(path = {"admin/sys/user_add"})
    public AjaxResult addUser(String username, String sex, String birthday,
                              String email, String state, String password) throws ParseException {
        User user = new User();
        user.setUsername(username)
                .setSex(sex)
                .setBirthday(DateUtil.strToDate(birthday))
                .setEmail(email)
                .setState(Integer.parseInt(state))
                .setPassword(password);
        return userService.addUser(user) ? AjaxResult.success() : AjaxResult.error();
    }

    @ResponseBody
    @PostMapping(path = {"admin/sys/user_del/{id}"})
    public AjaxResult deletedUserById(@PathVariable String id) throws ParseException {
        return userService.removeById(id) ? AjaxResult.success() : AjaxResult.error();
    }

    @ResponseBody
    @PostMapping(path = {"admin/sys/user/resetPsw"})
    public AjaxResult resetPsw(String id, String password) {
        boolean b = userService.updateById(new User().setId(id).setPassword(Md5Util.encodeByMd5(password)));
        return b ? AjaxResult.success() : AjaxResult.error();
    }

    @GetMapping(path = {"admin/sys/user_email/{id}"})
    public String sendEmail(@PathVariable String id, Model model) {
        model.addAttribute("id", id);
        return "admin/sys/user_email";
    }

    @ResponseBody
    @PostMapping(path = {"admin/sys/user_email"})
    public AjaxResult sendEmailToUser(String id, String title, String desc) {
        userService.sendEmailToUser(id, title, desc);
        return AjaxResult.success();
    }

    @ResponseBody
    @GetMapping(path = {"admin/sys/user/export"})
    public void exportUserInFo(HttpServletResponse response) throws IOException {
        exportUserInFo.export(response);
    }
}
